﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using CheatMenu.Utils;
using CheatMenu.Managers;
using Logger = BepInEx.Logger;
using BepInEx.Logging;
using System.IO;

namespace CheatMenu.Menus
{
    public class MenuDebug : MenuBase
    {
        GUIStyle defaultFontControlHeightButtonStyle;
        GUIStyle labelStyle;
        GUIStyle labelHeaderStyle;
        GUIStyle buttonStyle;

        MenuPrompt promptDumpAll;

        public MenuDebug() : base("Debug", null)
        {
            Alignment = Alignment.TopRight;

            promptDumpAll = new MenuPrompt("Dump All")
            {
                PromptText = "Are you sure you want to dump all scenes?\nThis can take several minutes",
                PromptButtons = { DialogResult.Yes, DialogResult.Cancel },
            };
            promptDumpAll.ButtonClicked += (s, b) =>
            {
                if (b == DialogResult.Yes)
                {
                    MenuManager.HideAll();
                    StateManager.DumpAllScenes();
                }
            };
        }

        protected override Vector2 InitializeSize()
        => new Vector2(GUIManager.ControlWidth + (GUIManager.PaddingSize * 2), (GUIManager.ControlHeight * 2) + (GUIManager.PaddingSize * 2));

        protected override void OnHidden() => promptDumpAll.Shown = false;

        protected override void InitializeGUI()
        {
            defaultFontControlHeightButtonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                fixedHeight = GUIManager.ControlHeight
            };

            labelStyle = new GUIStyle(GUI.skin.label)
            {
                fixedHeight = GUIManager.ControlHeight,
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelColor
                }
            };

            labelHeaderStyle = new GUIStyle(GUI.skin.label)
            {
                fixedHeight = GUIManager.ControlHeight,
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fontStyle = FontStyle.Bold,
                fontSize = GUIManager.ControlFontSize,
                normal = new GUIStyleState
                {
                    textColor = GUIManager.LabelHeaderColor
                }
            };

            buttonStyle = new GUIStyle(GUI.skin.button)
            {
                fontSize = GUIManager.ControlFontSize,
                margin = new RectOffset(0, 0, 0, 0),
                padding = new RectOffset(0, 0, 0, 0),
                fixedHeight = GUIManager.ControlHeight
            };
        }

        protected override void MenuWindow(int id)
        {
            GUILayout.BeginVertical();
            {
                GUILayout.BeginHorizontal();
                {
                    //GUILayout.Space(GuiManager.DefaultControlWidth);
                    GUILayout.FlexibleSpace();
                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        if (GUILayout.Button("Close", defaultFontControlHeightButtonStyle))
                        {
                            Shown = false;
                        }
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginVertical(GUI.skin.box);
                {
                    GUILayout.BeginVertical(GUI.skin.box);
                    {
                        GUILayout.Label("Scene", labelHeaderStyle);
                        GUILayout.Label(StateManager.LastSceneName, labelStyle);
                    }
                    GUILayout.EndVertical();

                    GUILayout.BeginVertical(GUI.skin.box);
                    {
                        GUILayout.Label("SceneDump", labelHeaderStyle);
                        if (GUILayout.Button("Dump Current", buttonStyle))
                        {
                            SceneUtil.DumpScene();
                            MenuManager.GetMenu<MenuNotifications>().PushNotification($"SceneDump: {StateManager.LastSceneName}", $"{Path.Combine(Application.dataPath, StateManager.LastSceneName + ".dmp")}");
                        }
                        if (GUILayout.Button("Dump All", buttonStyle)) { promptDumpAll.ToggleShown(); }
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        GUILayout.Label("FlowCharts", labelHeaderStyle);
                        GUILayout.FlexibleSpace();
                        GUILayout.Label($"{StateManager.FlowchartCount}", labelStyle);
                    }
                    GUILayout.EndHorizontal();

                    GUILayout.BeginHorizontal(GUI.skin.box);
                    {
                        GUILayout.Label("InitCounter", labelHeaderStyle);
                        GUILayout.FlexibleSpace();
                        GUILayout.Label($"{StateManager.InitCounter}", labelStyle);
                    }
                    GUILayout.EndHorizontal();
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndVertical();
        }
    }
}
